<?php

namespace app\api\model;

use app\api\model\Goods\Speci\Brand;
use app\api\model\Goods\Speci\Category;
use app\api\model\Goods\Speci\Color;
use app\api\model\Goods\Speci\Material;
use app\api\model\Goods\Speci\Season;
use app\api\model\Goods\Speci\Size;
use app\api\model\Goods\Speci\Style;
use think\db\Query;
use think\Model;
use think\Db;

class Goods extends Model
{
    use CanGetFieldModel, \app\admin\service\SystemLog;
    protected $prefix = 'jxc';
    protected $pk = 'id';
    protected $field = ['id','name','item','goods_code','images','brand','category','color',
    'size','num','trade_price','retail_price','recom_price','style','material','season','sex','year',
    'mini','sort','status','stock_warning','stock_upper','stock_lower','des','purchase_price'];

    public function warehouses()
    {
        return $this->belongsToMany(Warehouse::class, WarehouseGoods::class);
    }

    protected function getImagesAttr($value, $data)
    {
        return explode(',', $data['images'])[0];
    }

    protected function getBrandAttr($value, $data)
    {
        return Brand::where('id', $data['brand'])->value('name');
    }

    protected function getCategoryAttr($value, $data)
    {
        return Category::where('id', $data['brand'])->value('name');
//        return $this->getValue('category',['id'=>$data['category']],'name');
    }

    protected function getColorAttr($value, $data)
    {
        return Color::where('id', $data['brand'])->value('name');
//        return $this->getValue('color3',['id'=>$data['color']],'name');
    }

    protected function getSizeAttr($value, $data)
    {
        return Size::where('id', $data['brand'])->value('name');
//        return $this->getValue('color3',['id'=>$data['size']],'name');
    }
    protected function getStyleAttr($value, $data)
    {
        return Style::where('id', $data['brand'])->value('name');
//        return $this->getValue('color1',['id'=>$data['style']],'name');
    }

    protected function getMaterialAttr($value, $data)
    {
        return Material::where('id', $data['brand'])->value('name');
//        return $this->getValue('color1',['id'=>$data['material']],'name');
    }

    protected function getSeasonAttr($value, $data)
    {
        return Season::where('id', $data['brand'])->value('name');
//        return $this->getValue('color1',['id'=>$data['season']],'name');
    }

    public function searchBrandAttr(Query $query, $value, $data)
    {
        $query->whereLike('brand', "%" . $value . "%");
    }

    public function searchCategoryAttr(Query $query, $value, $data)
    {
        $query->whereLike('category', "%" . $value . "%");
    }
    public function searchNameAttr(Query $query, $value, $data)
    {
        $query->whereLike('name', "%" . $value . "%");
    }

    public function searchItemAttr(Query $query, $value, $data)
    {
        $query->whereLike('item', "%" . $value . "%");
    }

    public function searchGoodsCodeAttr(Query $query, $value, $data)
    {
        $query->whereLike('goods_code', "%" . $value . "%");
    }

    public function searchStatusAttr(Query $query, $value, $data)
    {
        $query->whereLike('status', "%" . $value . "%");
    }

    /**
     * @param $data
     * @param int $page
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\exception\DbException
     */
    public function getList($data, $page = 20)
    {
        if (!empty($data)) {
            $list = $this->withSearch(array_keys($data), $data)->order('sort asc')->paginate($page);
        } else {
            $list = $this->order('sort asc')->paginate($page);
        }
        return $list;
    }

    public function getInfo($id)
    {
        return $this->find($id);
    }

    public function getGoodsEdit($id, $data)
    {
//        $list = $this->getEdit('goods',['id'=>$id],$data);

        return self::where('id', $id)->update($data);
    }

    public function getGoodsAdd($data,$spe)
    {
        foreach($spe as $k=>$v){
            $data['color'] =$v['color'];
            $data['size'] =$v['size'];
            $data['style'] =$v['style'];
            $this->getAdd('goods',$data);
        }
        return 1;
    }

    public function getGoodsDel($id)
    {
        //TODO::  这里应该判断商品库存
        $arr = self::where('id', $id)->delete();
        return $arr;
    }

    /**
     * 商品新增 获取分类
     */
    public function getCate()
    {
        $cate = $this->getselectList('category','id,name,pid',[]);
        $list['cate'] = $this->getTree($cate);
        $list['brand'] = $this->getselectList('color1','*',['pid'=>3]);
        $list['material'] = $this->getselectList('color1','*',['pid'=>4]);
        $list['unit'] = $this->getselectList('color1','*',['pid'=>7]);
        $list['season'] = $this->getselectList('color1','*',['pid'=>6]);
        $list['style'] = $this->getselectList('color1','*',['pid'=>5]);
        $list['color'] = Db::name('color2')->alias('c2')->join('color3 c3', 'c3.pid=c2.gid')->where('c2.level', 1)->select();
        $list['size'] = Db::name('color2')->alias('c2')->join('color3 c3', 'c3.pid=c2.gid')->where('c2.level', 2)->select();
        return $list;
    }

    /**
     * 递归
     * @param $data
     * @param int $pId
     * @return array
     */
    function getTree($data, $pId = 0)
    {
        $tree = [];
        foreach ($data as $k => $v) {
            if ($v['pid'] == $pId) {
                $v['son'] = $this->getTree($data, $v['id']);
                $tree[] = $v;
                unset($data[$k]);
            }
        }
        return $tree;
    }
}